package com.kingmed.kmss.admin.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingmed.kmss.admin.modules.sys.mapper.SysOpenUrlMapper;
import com.kingmed.kmss.admin.modules.sys.model.SysOpenUrl;
import com.kingmed.kmss.admin.modules.sys.service.ISysOpenRelationService;
import com.kingmed.kmss.admin.modules.sys.service.ISysOpenUrlService;
import com.kingmed.kmss.common.constant.common.CommonConstants;
import com.kingmed.kmss.common.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 对外授权接口清单Service业务层处理
 *
 * @author Sys
 * @date 2022-12-16
 */
@Service
public class SysOpenUrlServiceImpl extends ServiceImpl<SysOpenUrlMapper, SysOpenUrl> implements ISysOpenUrlService {
    @Autowired
    private SysOpenUrlMapper sysOpenUrlMapper;
    @Autowired
    @Lazy
    private ISysOpenRelationService openRelationService;

    /**
     * 查询对外授权接口清单
     *
     * @param urlId 对外授权接口清单主键
     * @return 对外授权接口清单
     */
    @Override
    public SysOpenUrl selectSysOpenUrlByUrlId(String urlId) {
        return getById(urlId);
    }

    /**
     * 查询对外授权接口清单列表
     *
     * @param sysOpenUrl 对外授权接口清单
     * @return 对外授权接口清单
     */
    @Override
    public List<SysOpenUrl> selectSysOpenUrlList(SysOpenUrl sysOpenUrl) {
        return sysOpenUrlMapper.selectSysOpenUrlList(sysOpenUrl);
    }

    /**
     * 分页
     * @param sysOpenUrl
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<SysOpenUrl> list(SysOpenUrl sysOpenUrl, Integer pageNum, Integer pageSize) {
        Page<SysOpenUrl> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysOpenUrl> wrapper = this.initBaseQueryWrapper(sysOpenUrl);
        return page(page, wrapper);
    }

    /**
     * 新增对外授权接口清单
     *
     * @param sysOpenUrl 对外授权接口清单
     * @return 结果
     */
    @Override
    public boolean insertSysOpenUrl(SysOpenUrl sysOpenUrl) {
        checkIsExist(sysOpenUrl, true);
        return save(sysOpenUrl);
    }

    /**
     * 修改对外授权接口清单
     *
     * @param sysOpenUrl 对外授权接口清单
     * @return 结果
     */
    @Override
    public boolean updateSysOpenUrl(SysOpenUrl sysOpenUrl) {
        checkIsExist(sysOpenUrl, false);
        if (updateById(sysOpenUrl)){
            openRelationService.loadingCache();
            return true;
        }
        return false;
    }

    /**
     * 批量删除对外授权接口清单
     *
     * @param urlIds 需要删除的对外授权接口清单主键
     * @return 结果
     */
    @Override
    public boolean deleteSysOpenUrlByUrlIds(String[] urlIds) {
        if (urlIds == null || urlIds.length == 0) {
            return false;
        }
        List<String> urlIdList = (List<String>) Arrays.asList(urlIds);
        if (removeByIds(urlIdList)){
            openRelationService.deleteByUrlIds(urlIdList);
            return true;
        }
        return false;
    }

    /**
     * 删除对外授权接口清单信息
     *
     * @param urlId 对外授权接口清单主键
     * @return 结果
     */
    @Override
    public boolean deleteSysOpenUrlByUrlId(String urlId) {
        if (StringUtils.isBlank(urlId)) {
            return false;
        }
        if (removeById(urlId)){
            openRelationService.deleteByUrlId(urlId);
            return true;
        }
        return false;
    }

    /**
     * 根据主键集合获取数据
     *
     * @return
     */
    @Override
    public Map<Long, SysOpenUrl> listByIdsReturnMap(List<Long> ids) {
        List<SysOpenUrl> list = listByIds(ids);
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<>();
        }
        return list.stream().collect(Collectors.toMap(p -> p.getUrlId(), p -> p));
    }

    /**
     * 检查是否存在
     *
     * @return
     */
    private boolean checkIsExist(SysOpenUrl sysOpenUrl, boolean isAdd) {
        LambdaQueryWrapper<SysOpenUrl> wrapper = null;
        // 修改需要把主键设置为null
        SysOpenUrl updateQuery = new SysOpenUrl();
        updateQuery.setName(sysOpenUrl.getName());
        if (!isAdd) {
            wrapper = initBaseQueryWrapper(updateQuery);
        } else {
            wrapper = initBaseQueryWrapper(sysOpenUrl);
        }
        wrapper.last(CommonConstants.LIMIT_ONE);
        SysOpenUrl SysOpenUrlEntity = getOne(wrapper);
        if (null != SysOpenUrlEntity) {
            if (null != sysOpenUrl.getUrlId() || !SysOpenUrlEntity.getUrlId().equals(sysOpenUrl.getUrlId())) {
                throw new BusinessException("该信息已存在，请勿重复添加");
            }
        }
        return false;
    }

    /**
     * 初始化实体查询
     *
     * @return
     */
    private LambdaQueryWrapper<SysOpenUrl> initBaseQueryWrapper(SysOpenUrl sysopenurl) {
        LambdaQueryWrapper<SysOpenUrl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != sysopenurl.getUrlId(), SysOpenUrl::getUrlId, sysopenurl.getUrlId());
        wrapper.eq(StringUtils.isNotBlank(sysopenurl.getPath()), SysOpenUrl::getPath, sysopenurl.getPath());
        wrapper.eq(StringUtils.isNotBlank(sysopenurl.getName()), SysOpenUrl::getName, sysopenurl.getName());
        wrapper.eq(StringUtils.isNotBlank(sysopenurl.getPId()), SysOpenUrl::getPId, sysopenurl.getPId());
        wrapper.eq(StringUtils.isNotBlank(sysopenurl.getStatus()), SysOpenUrl::getStatus, sysopenurl.getStatus());
        wrapper.eq(StringUtils.isNotBlank(sysopenurl.getCreateBy()), SysOpenUrl::getCreateBy, sysopenurl.getCreateBy());
        wrapper.eq(null != sysopenurl.getCreateTime(), SysOpenUrl::getCreateTime, sysopenurl.getCreateTime());
        wrapper.eq(StringUtils.isNotBlank(sysopenurl.getUpdateBy()), SysOpenUrl::getUpdateBy, sysopenurl.getUpdateBy());
        wrapper.eq(null != sysopenurl.getUpdateTime(), SysOpenUrl::getUpdateTime, sysopenurl.getUpdateTime());
        return wrapper;
    }

}
